package com.bra.ui.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bra.R;
import com.bra.bean.HomeMessageBean;
import com.bra.bean.HomeType;
import com.bra.chat.ui.AddContactActivity;
import com.bra.lunbo.CarouselPageAdapter;
import com.bra.lunbo.CirclePageIndicator;
import com.bra.lunbo.MainPage;
import com.bra.request.UrlConstants;
import com.bra.ui.activity.MyMsgListActivity;
import com.bra.ui.activity.ArticleListActivity;
import com.bra.ui.activity.TopicActivity;
import com.bra.ui.adapter.Home_Type_adapter;
import com.bra.ui.base.BaseFragment;
import com.bra.utils.Contacts;


/**
 * 首页的fragment
 * 
 * @author lenovo
 *
 */
public class HomeFragment extends BaseFragment {

	private TextView h_address, h_xiaoxin,home_topic;
	// 图片轮播
	private RelativeLayout ll_container;
	private View mGallayView;
	private AutoScrollViewPager mGallayViewPage;
	private CarouselPageAdapter mGalleryAdapter;
	private CirclePageIndicator mGallayIndicator;
	private ArrayList<MainPage> bannerList;
	private ListView home_lv;
	private Home_Type_adapter adapter;
	private String HomeUrl = UrlConstants.IP;
	private MainPage mainpage;
	private List<MainPage> mainpags;
	private HomeMessageBean homepage;
	private List<HomeMessageBean> homepages;
	private TextView home_huodong;
	private List<HomeType> type_lists;
	private HomeType type_list;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return getRootView(R.layout.fragment_home);
	}

	@Override
	protected void fillData() {

	}

	@Override
	protected void findView() {
		mRequestQueue = Volley.newRequestQueue(getActivity());
		bannerList = new ArrayList<MainPage>();
		mainpage = new MainPage();
		h_address = (TextView) rootView.findViewById(R.id.h_address);
		h_xiaoxin = (TextView) rootView.findViewById(R.id.h_xiaoxin);
		home_topic= (TextView) rootView.findViewById(R.id.home_topic);
		ll_container = (RelativeLayout) rootView
				.findViewById(R.id.ll_container);
		home_lv = (ListView) rootView.findViewById(R.id.home_lv);
		home_huodong = (TextView) rootView.findViewById(R.id.home_huodong);
		lister();
		httpRequestHomeInfo(getActivity(), HomeUrl, RequestParams());
		// new HomeRequest().homerequest(mRequestQueue, HomeUrl, mHandler,
		// RequestParams());
	}

	private View.OnClickListener click = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.h_address:
				Toast.makeText(getActivity(), "未开发,进入添加好友界面", 0).show();
				Intent goodfrend = new Intent(getActivity(),
						AddContactActivity.class);
				startActivity(goodfrend);
				break;
			case R.id.h_xiaoxin:

				/*
				 * Intent intent=new Intent(getActivity(),GroupsActivity.class);
				 * startActivity(intent);
				 */

				Intent intent = new Intent(getActivity(),
						MyMsgListActivity.class);
				startActivity(intent);

				break;
			case R.id.home_huodong:
				Intent zixun = new Intent(getActivity(),
						ArticleListActivity.class);
				startActivity(zixun);

				break;
			case R.id.home_topic:
				Intent topic = new Intent(getActivity(),
						TopicActivity.class);
				startActivity(topic);

				break;

			default:
				break;
			}

		}
	};

	// 初始化点击事件
	public void lister() {
		h_address.setOnClickListener(click);
		h_xiaoxin.setOnClickListener(click);
		home_huodong.setOnClickListener(click);
		home_topic.setOnClickListener(click);
	}

	/**
	 * 加载轮播图
	 */

	public void initIndicator(Context context) {
		try {
			mGallayView = LayoutInflater.from(context).inflate(
					R.layout.view_gallery, null);
			mGallayViewPage = (AutoScrollViewPager) mGallayView
					.findViewById(R.id.id_content_viewpage);
			mGallayViewPage.setInterval(3000);
			mGallayViewPage.setScrollDurationFactor(3);
			mGallayIndicator = (CirclePageIndicator) mGallayView
					.findViewById(R.id.id_content_viewpage_indicator);
			mGalleryAdapter = new CarouselPageAdapter(getActivity(), bannerList);
			mGallayViewPage.setAdapter(mGalleryAdapter);
			mGallayIndicator.setViewPager(mGallayViewPage);
			mGalleryAdapter.notifyDataSetChanged();
			mGallayIndicator.notifyDataSetChanged();
			mGallayViewPage.startAutoScroll();
			ll_container.addView(mGallayView);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private RequestQueue mRequestQueue;

	// 首页的网络请求
	private void httpRequestHomeInfo(Context context, String url,
			final HashMap<String, String> params) {
		StringRequest req = new StringRequest(Request.Method.POST, url,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						try {
							System.out.println(response.toString() + "===");
							homepages = new ArrayList<HomeMessageBean>();
							type_lists = new ArrayList<HomeType>();
							JSONObject object = new JSONObject(response
									.toString());
							JSONArray data_array = object.getJSONArray("data");
							for (int i = 0; i < data_array.length(); i++) {
								JSONObject dataobject = data_array
										.getJSONObject(i);
								String id = dataobject.getString("id");
								String title = dataobject.getString("title");
								type_list = new HomeType(id, title);
								/*
								 * JSONArray
								 * list_array=dataobject.getJSONArray("value");
								 * //
								 * System.out.println(id+"=="+title+"=="+list_array
								 * .toString());
								 * 
								 * for (int j = 0; j< list_array.length()-1;
								 * j++) { JSONObject objecthsc =
								 * list_array.getJSONObject(i); String hscid =
								 * objecthsc.getString("id"); String hsctitle =
								 * objecthsc.getString("title"); String
								 * hscimageSrc =
								 * objecthsc.getString("imageSrc"); String
								 * hscdescription =
								 * objecthsc.getString("description"); String
								 * hscurl=objecthsc.getString("url"); homepage =
								 * new
								 * Home_ImageBean(hscid,hsctitle,hscimageSrc,
								 * hscdescription,hscurl);
								 * homepages.add(homepage); }
								 */
								type_lists.add(type_list);
							}
							// 配数据
							if (adapter == null) {
								adapter = new Home_Type_adapter(getActivity(),
										type_lists/* ,homepages */);
							}
							home_lv.setAdapter(adapter);
							initIndicator(getActivity());

							return;
						} catch (Exception e) {
							e.printStackTrace();
						}
						// Toast.makeText(getActivity(), "数据异常，请检查网络",
						// 0).show();
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError volleyError) {
						// Toast.makeText(getActivity(), "访问失败，请检查网络",
						// 0).show();
					}
				}) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				Map<String, String> map = params;
				return map;
			}
		};
		mRequestQueue.add(req);
	}

	private HashMap<String, String> RequestParams() {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("userId", "");
		map.put("equipment", "android");
		map.put("requestType", "Home_Api");
		map.put("token", "");
		return map;
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Contacts.MSG_GET_regist_INFO_SUCCESS:
				/*
				 * int stype = msg.arg1; if (stype == 1) {// 商铺 List<CouponInfo>
				 * couponInfos = (List<CouponInfo>) msg.obj; if (adapter ==
				 * null) { adapter = new
				 * Shop_DP_Adapter(getApplicationContext(), couponInfos); } else
				 * { if (shopPageIndex == 0) { adapter.setData(couponInfos); }
				 * else { adapter.addData(couponInfos); } }
				 * DIANPU_GV.setAdapter(adapter);
				 */
				break;
			case Contacts.MSG_GET_regist_INFO_FAILURE:
				Toast.makeText(getActivity(), "访问失败，请检查网络", 0).show();
				break;
			default:
				break;
			}
		};
	};
}
